﻿
namespace EAI.ServiceHost
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;

    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Description;

    using System.Security.Cryptography.X509Certificates;

    using EAI.Logging;

    public class EAIServiceManager
    {
        private EAIServiceManager() { }

        public static readonly EAIServiceManager Instance = new EAIServiceManager();

        public List<ServiceHost> hosts = new List<ServiceHost>();

        public void Start(EAIServiceConfig config, params string[] categorys)
        {
            if (config != null && config.Categories != null)
            {
                foreach (EAIServiceCategory category in config.Categories)
                {
                    try
                    {
                        if (categorys != null)
                        {
                            if (categorys.ToList().Exists(obj => string.Equals(category.Categroy, obj, StringComparison.OrdinalIgnoreCase)))
                            {
                                continue;
                            }
                        }

                        Log4NetHelper.GetLogger().Info(string.Format("{0} is starting!", category.Categroy));

                        Uri baseAddress = new Uri(category.BaseAddress);
                        foreach (EAIServiceItem item in category.Services)
                        {
                            try
                            {
                                Type type = Type.GetType(item.Service);
                                Type contract = Type.GetType(item.Contract);

                                ServiceHost serviceHost = new ServiceHost(type, baseAddress);

                                serviceHost.AddServiceEndpoint(
                                    contract,
                                    new CustomBinding(item.Binding),
                                    item.Name);
                                                          
                                //ServiceDebugBehavior debugBehavior = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();                           

                               serviceHost.Open();

                                Log4NetHelper.GetLogger().Info(string.Format("{0} service start successfully!", item.Name));

                            }
                            catch (Exception ex)
                            {
                                Log4NetHelper.GetLogger().Error(string.Format("{0} service start failed!", item.Name), ex);
                            }
                        }

                        Log4NetHelper.GetLogger().Info(string.Format("{0} start finished!", category.Categroy));
                    }
                    catch (Exception ex)
                    {
                        Log4NetHelper.GetLogger().Error(string.Format("{0} category start failed!", category.Categroy), ex);
                    }
                }
            }
        }

        public void Stop(EAIServiceConfig config, params string[] categorys)
        {
            Log4NetHelper.GetLogger().Info("Services begin stop!");

            foreach (ServiceHost host in hosts)
            {
                try
                {
                    host.Close();
                    (host as IDisposable).Dispose();
                }
                catch (Exception ex)
                {
                    Log4NetHelper.GetLogger().Error("Services stop failed!", ex);
                }
            }

            Log4NetHelper.GetLogger().Info("Services stop finished!");
        }
    }
}
